package controller

import (
	"go_web_todo/pkg/utils"
	"go_web_todo/service"
	"net/http"

	"github.com/gin-gonic/gin"
)

// @Tags TASK
// @Summary 创建任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param data body service.CreateTaskService true  "title"
// @Success 200 {object} serializer.ResponseTask "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /task [post]
func CreateTask(c *gin.Context) {
	var createTaskService service.CreateTaskService
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBindJSON(&createTaskService); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		res := createTaskService.CreateTask(chaim.Id)
		c.JSON(http.StatusOK, res)
	}
}

// @Tags TASK
// @Summary 获取任务列表
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param data body service.TaskListService true "rush"
// @Success 200 {object} serializer.ResponseTask "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /tasks [get]
func GetTaskList(c *gin.Context) {
	var taskListService service.TaskListService
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBind(&taskListService); err != nil { //ShouldBind参数放在queryString中,ShouldBindJSON则将参数放在Body中
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		res := taskListService.GetTaskList(chaim.Id)
		c.JSON(http.StatusOK, res)
	}
}

// @Tags TASK
// @Summary 展示任务详细信息
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param data body service.ShowTaskService true "rush"
// @Success 200 {object} serializer.ResponseTask "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /task/:id [get]
func ShowTaskDetails(c *gin.Context) {
	showTaskService := service.ShowTaskService{}
	res := showTaskService.ShowTaskDetails(c.Param("id"))
	c.JSON(http.StatusOK, res)
}

// @Tags TASK
// @Summary 修改任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param	data	body	service.UpdateTaskService true "2"
// @Success 200 {object} serializer.Response "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /task [put]
func UpdateTask(c *gin.Context) {
	var updateTaskService service.UpdateTaskService
	if err := c.ShouldBindJSON(&updateTaskService); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		res := updateTaskService.UpdateTask(c.Param("id"))
		c.JSON(http.StatusOK, res)
	}
}

// @Tags TASK
// @Summary 根据关键词搜索用户的备忘录
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param data body service.SearchTaskService true "2"
// @Success 200 {object} serializer.Response "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /search [post]
func SearchTasks(c *gin.Context) {
	var searchTaskService service.SearchTaskService
	chaim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBindJSON(&searchTaskService); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		res := searchTaskService.SearchTask(chaim.Id)
		c.JSON(http.StatusOK, res)
	}
}

// @Tags TASK
// @Summary 删除任务
// @Produce json
// @Accept json
// @Header 200 {string} Authorization "必备"
// @Param data body service.DeleteTaskService true "用户信息"
// @Success 200 {object} serializer.Response "{"success":true,"data":{},"msg":"ok"}"
// @Failure 500 {json} {"status":500,"data":{},"Msg":{},"Error":"error"}
// @Router /task/:id [delete]
func DeleteTaskById(c *gin.Context) {
	deleteTaskService := service.DeleteTaskService{}
	res := deleteTaskService.DeleteTaskById(c.Param("id"))
	c.JSON(http.StatusOK, res)
}
